AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Use(activator, caller)
	if (activator == self.dt.Owner || IsAllied(activator, self.dt.Owner)) && !self.World && !self.dt.Active then self:CreateTimers() end
	self.Active = true
	self:SetColor(255, 255, 255, 255)
end

function ENT:MakeMoney()
	if !self.World && self.Active && self.dt.Power >= self.Power then
		local trace = {}
		trace.start = self:GetPos()+self:GetAngles():Up() * 15;
		trace.endpos = trace.start + self:GetAngles():Forward() * -20 + self:GetAngles():Right() * -25
		trace.filter = self.Entity
		
		local tr = util.TraceLine(trace)
		local amount = math.random(self.PayMin, self.PayMax)
	
		
		local moneybag = ents.Create("item_money")
		moneybag:SetPos(tr.HitPos)
		moneybag:SetAngles(self:GetAngles())
		moneybag:SetColor(200, 255, 200, 255)
		moneybag:Spawn()
		
		moneybag.Value = amount
		Notify(self.dt.Owner, 0, 3, string.format("Money Printer created $%s!", amount))
	elseif !self.Active then
		Notify(self.dt.Owner, 4, 3, string.format("Your %s is inactive, so no money for you!", self.PrintName))
	elseif self.dt.Power < self.Power then
		Notify(self.dt.Owner, 4, 3, string.format("Your %s does not have enough power.", self.PrintName))
	end
end

function ENT:CreateTimers()
	timer.Create(tostring(self) .. "makemoney", self.MoneyTime, 0, self.MakeMoney, self)
	timer.Create(tostring(self) .. "warnturnoff", self.TurnOffTime - (self.TurnOffTime * 0.1), 1, self.WarnTurnOff, self)
	timer.Create(tostring(self) .. "turnoff", self.TurnOffTime, 1, self.TurnOff, self)
end

function ENT:OnRemove()
	timer.Remove(tostring(self) .. "makemoney")
	timer.Remove(tostring(self) .. "warnturnoff")
	timer.Remove(tostring(self) .. "turnoff")
end

